/*
 * @Author: error: git config user.name && git config user.email & please set dead value or install git
 * @Date: 2022-07-19 00:07:46
 * @LastEditors: GaoYiNuo 1534815804@qq.com
 * @LastEditTime: 2022-07-24 22:57:30
 * @FilePath: \project\src\store\modules\reduce.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { MyAction, MyState } from '../../type/router.d'

const initialState = {
    titleData: [],
    detailArr: [],
    arcData: [],
    detaactArr: [],
    data: [], //留言板评论数据
    pageData: [], //留言板分页数据
    articleList: [], //文章列表数据
    articleTabList: [], //tab切换数据
    xiangList: [], //详情页数据
    lbtList: [], //轮播图数据
    //---关于页面数据
    listAll: [],//下边列表总数据
    list: [],//
    commentList: [],//获取评论总数据
    commentListSli: [],//截取后的数据
    pageSize2: 1,//评论列表的页码
    count2: 2,//评论每页显示2条
    detailList: [],//详情数据
    artichiveUserState:[]//登陆成功数据约束
}

const reduce = (state: MyState = initialState, { type, payload }: MyAction) => {
    let newState = JSON.parse(JSON.stringify(state))
    //    关于页面数据
    let { list, commentList, pageSize2, count2, commentListSli } = state;
    let start = (pageSize2 - 1) * count2;
    let end = pageSize2 * count2;
    switch (type) {
        //获取标题数据
        case "GETTITLEDATA":
            state.titleData = payload
            return { ...state, titleData: [...state.titleData] }
        //获取详情页数据
        case "GETDETAILDATA":
            state.detailArr = []
            state.detailArr.push(payload)
            return { ...state, detailArr: [...state.detailArr] }
        case "GETACTDATA_ACTION":
            state.arcData = payload
            return { ...state, arcData: [...state.arcData] }
        case "GETACTDETAILDATA":
            state.detaactArr = []
            state.detaactArr.push(payload)
            return { ...state, detaactArr: [...state.detaactArr] }
        // 留言板数据
        case 'GETDATA':
            newState.data = payload
            newState.pageData = payload.slice(0, 3)
            return newState
        //留言板分页
        case 'PAGDATA':
            newState.pageData = payload
            return newState

        case 'GETARTICLELIST': //获取文章列表
            state.articleList = payload;
            return {
                ...state,
                articleList: [...state.articleList],
            }
        case 'CHANGE_INDEX': //tab切换数据
            state.articleTabList = [];
            state.articleList.map((item, index) => {
                if (item.class === payload) {
                    state.articleTabList.push(item);
                } else if (payload === '所有') {
                    state.articleTabList.push(item);
                }
            })
            return {
                ...state,
                articleTabList: [...state.articleTabList],
            }
        case 'GETXIANGLIST': //详情页数据
            state.xiangList = [];
            state.xiangList.push(payload);
            return {
                ...state,
                xiangList: [...state.xiangList]
            }
        case 'GETLBTLIST': //获取轮播图数据
            state.lbtList = payload;
            return {
                ...state,
                lbtList: [...state.lbtList]
            }
        case 'GETXIANGLB'://轮播图详情
            console.log(payload);
            state.xiangList = [];
            state.xiangList.push(payload)
            return {
                ...state,
                xiangList: [...state.xiangList]
            }
        case 'ADDZAN': //点赞
            state.articleList.map((item, index) => {
                if (item.id === payload.id) {
                    item.zan++;
                    item.zanflag = true;
                }
            })
            return {
                ...state,
                articleList: [...state.articleList]
            }
        case 'ADDLOOK'://阅读
            state.articleList.map((item, index) => {
                if (item.id === payload.id) {
                    item.look++;
                }
            })
            return {
                ...state,
                articleList: [...state.articleList]
            }
        // 关于
        // ------ 下边关于页面文章数据列表
        case 'GET_LIST_ALL':
            state.listAll = payload;
            return {
                ...state,
            }
        case 'GET_LIST'://截取后的数组
            state.list = payload;
            return { ...state, ...payload }
        case 'GET_DETAIL':
            state.detailList = [];//清空详情数组
            state.listAll.map((item, index) => {
                if (item.id == payload) {
                    state.detailList.push(item);
                }
            })
            // state.detailList.push(payload)
            return {
                ...state,
                detailList: [...state.detailList]
            }
        // ------ 评论列表
        case 'GET_COMMENT_LIST':
            console.log(state.commentListSli, '截取的数据在仓库显示');
            state.commentList = payload;//拿到评论列表
            state.commentListSli = state.commentList.slice(start, end);
            console.log(commentListSli, '截取后的数组');
            return {
                ...state,
                commentList: [...state.commentList],
                commentListSli: [...state.commentListSli],
            }
        // 评论列表切换当前页码
        case 'CHANGE_COM_PAGE':
            state.pageSize2 = payload;//切换当前页码
            state.commentListSli = commentList.slice(start, end);
            console.log(commentListSli, '截取后的数组2');
            return {
                ...state,
                pageSize2: state.pageSize2,
                commentList: [...state.commentList],
                commentListSli: [...state.commentListSli],
            }
        //登陆成功保存数据
        case "STORAGE_USER_STATE":
            state.artichiveUserState=[]
            state.artichiveUserState.push(payload)
            return {...state,artichiveUserState:[...state.artichiveUserState]}
        default:
            return state
    }
}


// export default reducer

export default reduce


